package top.cluski.feieprint.framework;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.cluski.feieprint.config.AuthConfig;

@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Resource
    AuthConfig authConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (authConfig.getTokens().contains(token)) {
            return true;

        }

        response.sendError(401, "token is not allowed");
        return false;
    }
}
